<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.getFiles.php');

$aCamForm = array();
require("_inc/const.campiAsez.php");

//if (reqVar('azione') == 'salva') {
//	$msgPag = conferma($aCamForm);
//} 

$sqlSel = "SELECT s.* FROM ovf_anag_sez s WHERE s.cod_sez = '".reqVarAS('cod')."'";
$objDB->exec($sqlSel);
if(!$aRow = $objDB->fetch()) {
	backOnErr("id. errato");
}
if ($aRow['tipo_sez']!='ext') {
	$file = "ovf_sez_".reqVar('cod');
	$aFileX = getFiles($file);	
	//dbg_($aFileX);
	$aDefCampi = getDefCampi($file);
	if ($aDefCampi===false) {
		backOnErr("fileView - manca cod");
		exit();
	}
}
reqDef('cod_sez',$aRow['cod_sez']);
reqDef('descr',$aRow['descr']);
reqDef('tipo_sez',$aRow['tipo_sez']);
reqDef('seq',$aRow['seq']);
reqDef('url_ext',$aRow['url_ext']);

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"../file/fileCamIns.php?cod=".$file."&$sXSNQS"."\" >nuovo campo</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"asezMod.php?cod=".reqVarUE('cod')."&$sXSNQS"."\" >modifica</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione sezioni anagrafica','Visualizza sezione',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('cod');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Sezione anagrafica</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'vis');
if (reqvar('tipo_sez')=='ext') {
?>
   <tr>
      <td class="lab1" valign='top' colspan='4'>URL</td>
      <td class="txt1" colspan='16' align='left'>
			<?=reqvar('url_ext')?>
		</td>
   </tr>	
<?
} else {
?>
   <tr>
      <td class="lab1" colspan='4' align='left'>File</td>
      <td class="txt1" colspan='4'' align='left'>
			<font style='font-family:lucida console,courier;'><?=$file?></font>
		</td>
      <td class="txt1" colspan='12' align='left'>
			<?=$aFileX['descr']?>
		</td>
   </tr>
   <tr>
      <td class="lab1" valign='top' colspan='4'>Chiave primaria</td>
      <td class="txt1" colspan='16' align='left'>
			<?=$aFileX['primary']?>
		</td>
   </tr>
   <tr>
      <td class="lab1" align='center' colspan='3'>Campo</td>
      <td class="lab1" align='center' colspan='3'>Tipo</td>
      <td class="lab1" align='center' colspan='3'>Default</td>
      <td class="lab1" align='center' colspan='3'>&nbsp;</td>
      <td class="lab1" align='center' colspan='8'>Nota</td>
   </tr>
<?php 
	foreach($aDefCampi as $campo => $aDC) {
?>
   <tr>
      <td class="txt1" align='left' colspan='3'>
      	<?
      	$hrefX = "../file/fileCamMod.php?cod=".$file."&campo=$campo&$sXSNQS";
      	if (isset($aFileX['campiFix'][$campo])) {
      		echo "<b>$campo</b>";
      	} else {
      		echo "<a href='$hrefX' class='txt1'>$campo</a>";
      	}
      	?>
      </td>
      <td class="txt1" align='left' colspan='3'><?=$aDC[0]?></td>
      <td class="txt1" align='left' colspan='3'><?=$aDC[1]?></td>
      <td class="txt1" align='center' colspan='3'><?=$aDC[2]?></td>
      <td class="txt1" align='left' colspan='8'><?=$aDC[3]?></td>
   </tr>
<?php 
	}
?>
   <tr>
      <td class="lab1" align='center' colspan='3'>Indice</td>
      <td class="lab1" align='center' colspan='17'>Campi</td>
   </tr>
<?php 
	foreach($aFileX['index'] as $indice => $sCampiInd) {
?>
   <tr>
      <td class="txt1" align='left' colspan='3'><?=$indice?></td>
      <td class="txt1" align='left' colspan='17'><?=$sCampiInd?></td>
   </tr>
<?php 
	}
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_sez'] = strtolower(trim(reqVar('cod_sez')));
	if (reqVar('cod_sez')=='') {
		$msg .= "Codice obbligatorio<br>";
	}
	if (preg_match("/[^-_a-z0-9]/",reqVar('cod_sez'))) {
		$msg = "Codice '".reqVar('cod_sez')."' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	if (preg_match("/[^0-9]/",reqVar('seq')) || reqVarTrim('seq')=='') {
		$msg = "campo 'Sequenza' non numerico o vuoto<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	if (reqVar('cod_sez') != reqVar('cod')) {
		$sqlSel = "SELECT cod_sez FROM ovf_anag_sez " .
				" WHERE cod_sez = '".reqVarAS('cod_sez')."'";
		$objDB->exec($sqlSel,'insIst');
		$aRow = $objDB->fetch('insIst');
		if ($aRow) {
			$msg .= "Codice gia' esistente<br>";
		}
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "UPDATE ovf_anag_sez SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			$sSql .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
		}
	}
	$sSql = substr($sSql,0,-2)." WHERE cod_sez = '".reqVarAS('cod')."'";
	$objDB->exec($sSql,'insRec');
	
	header("Location: ".urlTorna(_t("sezione '%s' modificata",reqVar('cod_sez'))));
	exit();
}
